#include <iostream>
#include <string>
#include "util.hpp"
#include "user_table.hpp"
#include "online.hpp"
#include "room.hpp"
#include "session.hpp"
#include "matcher.hpp"
#include "server.hpp"

using namespace std;
using namespace ns_util;

void test_json_util()
{
    Json::Value stu;
    stu["name"] = "zhangsan";
    stu["age"] = 19;
    stu["socre"].append(77.5);
    stu["socre"].append(88);
    stu["socre"].append(99.5);
    string str;
    json_util::serialize(stu, str);
    cout << "序列化结果:" << str << endl;
    Json::Value root;
    json_util::unserialize(str, root);
    std::cout << "反序列化结果:\n"
              << "name:" << root["name"].asString() << "\n"
              << "age:" << root["age"].asInt() << "\n"
              << "socre:" << root["socre"][0].asFloat() << " " << root["socre"][1].asInt()
              << " " << root["socre"][2].asFloat() << "\n";
}

void test_string_util()
{
    std::string body;
    file_util::read("1.txt", body);
    std::cout << body << "\n";
    std::string sep = "1";
    std::vector<std::string> arry;
    string_util::split(body, sep, arry);
    for (auto str : arry)
    {
        std::cout << str << "\n";
    }
}

#define HOST "127.0.0.1"
#define USER "gobang"
#define PASSWD "123456"
#define DBNAME "online_gobang"

void test_mysql_util()
{
    MYSQL *mysql = mysql_util::mysql_create(HOST, USER, PASSWD, DBNAME, 3306);
    std::string sql = "insert into user values(null, 'xiaoduantui', MD5('123'), 1000, 0, 0);";
    mysql_util::mysql_exec(mysql, sql);
    mysql_util::mysql_destory(mysql);
}

void test_user_table()
{
    Json::Value user;
    user["username"] = "lxl";
    user["password"] = "123456";
    user_table *user_t = new user_table(HOST, USER, PASSWD, DBNAME);
    user_t->insert(user);
    std::cout << "insert:" << user << "\n";
    user_t->login(user);
    std::cout << "login:" << user << "\n";
    user_t->select_by_id(4, user);
    std::cout << "select_by_id: " << user << "\n";
    user_t->select_by_name("lxl", user);
    std::cout << " select_by_name: " << user << "\n";
    user_t->win(4);
    std::cout << "win:" << user << "\n";
    user_t->lose(4);
    std::cout << "lose:" << user << "\n";
}

void test_online()
{
    online_manager *om = new online_manager();
    uint64_t uid = 2;
    wsserver_t::connection_ptr con;
    om->enter_game_hall(uid, con);
    if (om->in_game_hall(uid) == false)
    {
        LOG(ERROR) << "no in game hall\n";
    }
    else
    {
        LOG(ERROR) << "in game hall\n";
    }

    uint64_t uid1 = 3;
    wsserver_t::connection_ptr con1;
    om->enter_game_room(uid1,con1);
    if(om->in_game_room(uid1) == false)
    {
        LOG(ERROR) << "no in game room\n";
    }
    else
    {
        LOG(ERROR) << "in game room\n";
    }
}
/*
{
"optype": "put_chess", // put_chess表⽰当前请求是下棋操作
"room_id": 222, // room_id 表⽰当前动作属于哪个房间
"uid": 1, // 当前的下棋操作是哪个⽤⼾发起的
"row": 3, // 当前下棋位置的⾏号
"col": 2 // 当前下棋位置的列号
}
*/

void test_room()
{
    uint64_t room_id = 222;
    user_table *tb_user = new user_table(HOST, USER, PASSWD, DBNAME);
    online_manager *online_user = new online_manager();
    room *room_t = new room(room_id, tb_user, online_user);
    Json::Value req;
    req["optype"] = "put_chess";
    req["room_id"] = 222;
    req["uid"] = 1;
    req["row"] = 3;
    req["col"] = 2;

    room_t->handle_request(req);
}

void test_session()
{
}

void test_matcher()
{
    user_table *ut = new user_table(HOST, USER, PASSWD, DBNAME); 
    online_manager *om;
    room_manager *rm = new room_manager(ut,om);
    matcher *mt = new matcher(rm,ut,om);

}
int main()
{
    gobang_server server(HOST, USER, PASSWD, DBNAME);
    server.start(8888); 
    return 0;
}